<?php  namespace Home\Controller;
use Hdphp\Controller\Controller;
use Home\Model\User;
class UserController extends CommonController{
    private $model;
    public function __init(){
        parent::__init();
        $this->model = new User;
    }
        //载入
    public function index(){
            //获取用户订单
        $uid = $_SESSION['uid'];
        $data = Db::table('orders')->where("user_uid={$uid}")->orderBy('time','DESC')->get();
            //把订单和订单列表关联
        foreach($data as $k => $v){
            $data[$k]['list'] = Db::table('order_list')->join("goods",'goods_gid','=','gid')->where("order_oid={$v['oid']}")->get();
        }
            //实例化订单表
        $orderModel = new \Home\Model\Orders;
            //订单总数量
        $all = $orderModel->where("user_uid={$uid}")->count();
            //未付款订单数量
        $noPay = $orderModel->where("status = '未付款' AND user_uid = {$uid}")->count();
            //已付款订单数量
        $pay = $orderModel->where("status = '已付款' AND user_uid = {$uid}")->count();
            //已发货数量
        $send = $orderModel->where("status = '已发货' AND user_uid = {$uid}")->count();
            //已完成数量
        $done = $orderModel->where("status = '已完成' AND user_uid = {$uid}")->count();
            //已取消数量
        $cancle = $orderModel->where("status = '已取消' AND user_uid = {$uid}")->count();
        $num = array(
            'all'       => $all,
            'noPay'     => $noPay,
            'pay'       => $pay,
            'send'      => $send,
            'done'      => $done,
            'cancle'    => $cancle
        );
            //筛选订单
        $n = Q('get.n',1,'intval');
        if($n == 1){
            $data = $data;
        }
        if($n == 2){
            $arr = array();
            foreach($data as $k => $v){
                if($v['status'] == '未付款'){
                    $arr[] = $v;
                }
            }
            $data = $arr;
        }
        if($n == 3){
            $arr = array();
            foreach($data as $k => $v){
                if($v['status'] == '已发货'){
                    $arr[] = $v;
                }
            }
            $data = $arr;
        }
        if($n == 4){
            $arr = array();
            foreach($data as $k => $v){
                if($v['status'] == '已完成'){
                    $arr[] = $v;
                }
            }
            $data = $arr;
        }
        if($n == 5){
            $arr = array();
            foreach($data as $k => $v){
                if($v['status'] == '已取消'){
                    $arr[] = $v;
                }
            }
            $data = $arr;
        }
        if($n == 6){
            $arr = array();
            foreach($data as $k => $v){
                if($v['status'] == '已付款'){
                    $arr[] = $v;
                }
            }
            $data = $arr;
        }
        View::with('num',$num);
            //获取商品信息
        View::with('data',$data);
        View::make();
    }
        //用户中心
    public function users(){
        //获取数据
        $uid = $_SESSION['uid'];
        $uData = Db::table('user')->where("uid={$uid}")->first();
            //抽出生日
        $birthday = $uData['birthday'];
            //拆分
        $b = explode('-', $birthday);
        $uData['year'] = $b[0];
        $uData['month'] = $b[1];
        $uData['day'] = $b[2];
        View::with('data',$uData);
        View::make();
    }
        //地址
    public function address(){
            if(IS_AJAX){
                $addModel = new \Home\Model\Address;
                if(!$addModel->store()){
                    return false;
                }
                echo 1;
                exit;
            }
         //获取地址
        $uid = $_SESSION['uid'];
        //实例化订单表
        $orderModel = new \Home\Model\Orders;
         //订单总数量
        $num['all'] = $orderModel->where("user_uid={$uid}")->count();
        $address = Db::table('user_address')->where("user_uid={$uid}")->get();
        View::with('address',$address);
        View::with('num',$num);
        View::make();
    }

    //修改
    public function edit(){
        if(IS_POST){
            //获取提交数据
            $data = Q('post.');
            $uid = Q('post.uid',0,'intval');
            $password = Q('post.password');
                //获取旧地址
            $ad = $this->model->where("uid={$uid}")->pluck('address');
                //如果地址修改了
            if(Q('post.sheng')){
                $data = array(
                    'uid'   => $uid,
                    'username'  => $data['username'],
                    'email'     =>  $data['email'],
                    'neckname'  =>  $data['neckname'],
                    'address'   => $data['sheng'].$data['shi'],
                    'sex'     => $data['sex'],
                    'birthday' => $data['year'].'-'.$data['month'].'-'.$data['day'],
                );
            }
                //如果地址没有修改
            if(!Q('post.sheng')){
                $data = array(
                    'uid'   => $uid,
                    'username'  => $data['username'],
                    'email'     =>  $data['email'],
                    'neckname'  =>  $data['neckname'],
                    'address'   => $ad,
                    'sex'     => $data['sex'],
                    'birthday' => $data['year'].'-'.$data['month'].'-'.$data['day'],
                );
            }
                //把昵称放入session
            $_SESSION['neckname'] = $data['neckname'];
            $this->model->where("uid={$uid}")->save($data);
            View::success('修改成功',U('index'));
        }

            
    }

        //修改密码
    public function changePwd(){
        if(IS_AJAX){
                //获取用户名，和用户ID
            $uid = $_SESSION['uid'];
            $username = $_SESSION['username'];
                //接受数据
            $data = Q('post.');
                //加密旧密码
            $oldPwd = $this->model->encrypt($username,$data['oldPwd']);
                //获得数据库里面的密码
            $oldPassword = $this->model->where("uid={$uid}")->pluck('password');
                //比对
            if($oldPwd != $oldPassword){
                echo json_encode("旧密码不正确");
                exit;
            }
                //如果正确,则获取两个新密码，比对
            $password = $this->model->encrypt($username,$data['password']);
            $confirmPwd = $this->model->encrypt($username,$data['confirmPwd']);
            if($password != $confirmPwd){
                echo json_encode('两次密码不一样');
                exit;
            }
            $new['password'] = $password;
            $this->model->where("uid={$uid}")->save($new);
            echo json_encode('修改成功');
            exit;
        }
    }
    //取消订单
    public function cancle(){
        $oid = Q('get.oid',0,'intval');
        $data['status'] = '已取消';
        $oModel = new \Home\Model\Orders;
        $oModel->where("oid={$oid}")->save($data);
        View::success('取消成功',U('index'));
    }
    //详情
    public function detail(){
        $uid = $_SESSION['uid'];
        $oid = Q('get.oid',0,'intval');
        //实例化订单表
        $orderModel = new \Home\Model\Orders;
            //订单总数量
        $num['all'] = $orderModel->where("user_uid={$uid}")->count();
            //获取订单详情
        $data = $orderModel->where("oid={$oid}")->find();
             //把订单和订单列表关联
        $data['list'] = Db::table('order_list')->join("goods",'goods_gid','=','gid')->where("order_oid={$oid}")->get();
        View::with('data',$data);
        View::with('num',$num);
        View::make();
    }

    //余额
    public function money(){
        //获取地址
        $uid = $_SESSION['uid'];
        //实例化订单表
        $orderModel = new \Home\Model\Orders;
         //订单总数量
        $num['all'] = $orderModel->where("user_uid={$uid}")->count();
        View::with('num',$num);
            //获取账户余额
        $money = Db::table('user')->where("uid = {$uid}")->pluck("money");
         //充值 
        if(IS_POST){
            $data = Q('post.money-add',0,'intval');
            $money = intval($data) + intval($money);
            $data = array(
                'uid'  => $uid,
                'money'  =>  $money
            );
            $this->model->where("uid={$uid}")->save($data);
        }

        View::with('money',$money);
        View::make();
    }




}



?>